package com.xca.is.model;

import android.util.Log;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.xca.is.entity.Courier;
import com.xca.is.http.HttpConnect;
import com.xca.is.repository.CourierRepository;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

public class CourierModel extends ViewModel {
    private CourierRepository repository ;
    private MutableLiveData<Courier> liveData = new MutableLiveData<>();
    private HttpConnect httpConnect ;

    public CourierModel(){
        repository = null ;
    }
    public void setModel(HttpConnect httpConnect){
        this.httpConnect = httpConnect ;
        this.repository = new CourierRepository() ;
    }

    public void getData(String num , String phone){
        repository.getData(num, phone)
                .subscribe(new Observer<Courier>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        httpConnect.Loading(true);
                        Log.d("数据获取", "数据获取中");
                    }

                    @Override
                    public void onNext(@NonNull Courier courier) {
                        if (courier.getCode()==200){
                            liveData.postValue(courier);
                        }else {
                            httpConnect.Error("???error???");
                        }
                        Log.d("数据", String.valueOf(courier.getCode()));
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.d("数据获取", "数据获取失败" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        httpConnect.Loading(false);
                        Log.d("数据获取", "数据获取完成");
                    }
                }) ;
    }
    public MutableLiveData<Courier> getLiveData(){
        return  liveData ;
    }
}
